package com.cf.test;

import java.io.*;
import java.util.Scanner;

/**
 * @author: chenfeng
 * @time: 2019/6/10 10:33
 */
public class FileCopy {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("输入源文件所在路径:");
        String sourcePath = in.next();
        System.out.println("输入要复制到的路径:");
        String targetPath = in.next();
        File sourceFile = new File(sourcePath);
        File targetFile = new File(targetPath);
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            Long startTime = System.currentTimeMillis();
            System.out.println("开始复制………………");
            inputStream = new FileInputStream(sourceFile);
            outputStream = new FileOutputStream(targetFile);
            byte[] buffer = new byte[1024 * 5];
            int byteRead = -1;
            while ((byteRead = (inputStream.read(buffer))) != -1){
                outputStream.write(buffer, 0, byteRead);
            }
            Long endTime = System.currentTimeMillis();
            System.out.println("复制完成！耗时"+(endTime - startTime)+"ms");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(outputStream != null){
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
    
}
